define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var popwin	= require('@base/dialog').base;
	var util	= require('util');
	/*
		批量批入批出基础模块, 批出实际为内容复制而已.
	 */

	var Export = popwin.extend({
		init:function(config){
			this.$title =
			config = pubjs.conf(config, {
				'title': '',
				'width': 700,
				'textRow': 15,
				"buttons":["cancel" , "ok"],
				"buttonConfig":{
				// 确定按钮样式
					"ok": {
						'value': LANG('复制')
					}
					,"cancel": {
						'value': LANG('关闭')
					}
				},
				'view_model': {
					'isAddMode': false
				}
			});

			this.Super('init', arguments);
		}
		,afterBuild:function(){
			this.Super('afterBuild',arguments);
			var c = this.getConfig();
			var el = this.getDOM('body');

			this.$text = $('<textarea style="width:100%; height:auto; padding:0;"/>').appendTo(el);
			if (c.textRow){
				this.$text.attr('rows', c.textRow);
			}

			// 绑定复制
			pubjs.customClipboard(this.$text, this.getDOM('foot').find('input[data-action="onOk"]'), this.copyComplete)

			this.buildFromTemplate();
		}
		,reset: function(){
			this.$text.val('');
			return this;
		}
		,setData: function(data) {
			this.$text.val(data);
			return this;
		}
		,copyComplete: function(){
			pubjs.alert(LANG('复制成功'));
			return false;
		}
	});

	exports.export = Export;

	var Import = popwin.extend({
		init:function(config){
			this.$title =
				config = pubjs.conf(config, {
					'title': '',
					'width': 700,
					'textRow': 15,
					"buttons":[ "cancel","ok"],
					"buttonConfig":{
						// 确定按钮样式
						"ok": {
							'value': LANG('导入')
						}
					},
					'view_model': {
						'isAddMode': false
					}
				});

			this.Super('init', arguments);
		}
		,afterBuild:function(){
			this.Super('afterBuild',arguments);
			var c = this.getConfig();
			var el = this.getDOM('body');

			this.$text = $('<textarea style="width:100%; height:auto; padding:0;"/>').appendTo(el);
			if (c.textRow){
				this.$text.attr('rows', c.textRow);
			}


			this.buildFromTemplate();
		}
		,reset: function(){
			this.$text.val('');
			return this;
		}
		,setData: function(data) {
			this.$text.val(data);
			return this;
		}
		,onOk: function() {
			var data = this.$text.val();
			var lines = data.split('\n');
			var list  = [];
			var datas = [];
			util.each(lines, function(line){
				var _64Regx = /^\#\w+$/;
				var id = util.trim(line.split('\t').shift());
				if (id && (!isNaN(+id || _64Regx.test(id)))){
					list.push(id);
					datas.push(line);
				}
			});

			if(list.length) {
				this.fire(
					"importList"
					,{
						"list":list
						,"lines":datas
					}
				);
			}

			this.hide();
			return false;
		}
	});

	exports.import = Import;
});